package com.多用户通讯编程.QQ.Server.QQservice;

import com.多用户通讯编程.QQ.Common.Message;
import com.多用户通讯编程.QQ.Common.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.Vector;

public class SendOfflineMessageThread extends Thread{
    private String userId;
    private Vector<Message> vector;

    public SendOfflineMessageThread(String userId, Vector<Message> vector) {
        this.userId = userId;
        this.vector = vector;
    }

    @Override
    public void run() {
        /// 用来将离线消息发送的线程；
        /// 与接收方关联的输出流；
        ObjectOutputStream oos = ManageServerConnectClientThread.get(userId).oos;
        /// 遍历消息队列
        Iterator<Message> iterator = vector.iterator();
        while (iterator.hasNext()){
            /// 拿到消息
            Message message = iterator.next();
            /// 因为在QQServer类中，保存完后，将返回message对象时被改变了，
            ///所以又将消息类型改成私聊
            try {
                oos.writeObject(message);
                oos.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


}
